package com.sinfonia.wipes.common.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InterfaceFilter implements Filter{
	private final static Logger log = LoggerFactory.getLogger(InterfaceFilter.class);
	
	/**
     * 封装，不需要过滤的list列表
     */
    protected final static List<Pattern> patterns = new ArrayList<Pattern>();
    protected final static List<String> urlPatterns = new ArrayList<String>();
//    protected final static String patternStrings = ".*[(\\.js)||(\\.css)||(\\.png)||(\\.gif)]||(\\.map)]";
    protected final static String patternStrings = ".*(\\.js|\\.css|\\.png|\\.gif|\\.map|\\.woff)";

	
	 	@Override
	    public void init(FilterConfig filterConfig) throws ServletException {
	 		//配置放行资源
	        patterns.add(Pattern.compile(patternStrings));
	        urlPatterns.add("/static/");
	        urlPatterns.add("/layuiadmin/");
	        urlPatterns.add("/font/");
	    }

	    @Override
	    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	                chain.doFilter(request, response);
	    }

	    @Override
	    public void destroy() {

	    }
	    /**
	     * 是否需要过滤
	     * @param url
	     * @return
	     */
	    private boolean isInclude(String url) {
	    	url=url.toLowerCase();
	        for (Pattern pattern : patterns) {
	            Matcher matcher = pattern.matcher(url);
	            if (matcher.matches()) {
	                return true;
	            }
	        }
	        for (String urlPattern : urlPatterns) {
	        	 if (url.contains(urlPattern)) {
	                 return true;
	             }
	        }
	        return false;
	    }

	public static void main(String[] args) {
	    	//||(*.css)||(*.png)||(*.gif)||(*.map)
		 String patternStrings = ".*(\\.js|\\.css|\\.png|\\.gif|\\.map)";
		 List<Pattern> patterns2 = new ArrayList<Pattern>();
		 patterns2.add(Pattern.compile(patternStrings));
		 String url = "/capital/capitalBq/notificationAuthorizedResults";
//		 String url = "/scyd/preAuthor/preauthTransfer";
//		 String url = "/remoteScyd/preAuthor/static?requestURI=preEmpowerment/handle";
//		 String url = "/scyd/preAuthor/createQrCodeUrl";
//		 String url = "/static/css/base.css";
		System.out.println(url);
		for (Pattern pattern : patterns2) {
		System.out.println(pattern.toString());
			Matcher matcher = pattern.matcher(url);
			if (matcher.matches()) {
				System.out.println("匹配成功");
			}
		}
	}
}